Full source code website bán hàng thương mại điện tử gần giống shopee
473.067 lượt xem;
1 (function($) {
2 $.fn.shake = function(o) {
3 if (typeof o === 'function')
4 o = {callback: o};
5 // Set options
6 var o = $.extend({
7 direction: "left",
8 distance: 20,
9 times: 3,
10 speed: 140,
11 easing: "swing"
12 }, o);
13
14 return this.each(function() {
15
16 // Create element
17 var el = $(this), props = {
18 position: el.css("position"),
19 top: el.css("top"),
20 bottom: el.css("bottom"),
21 left: el.css("left"),
22 right: el.css("right")
23 };
24
25 el.css("position", "relative");
26
27 // Adjust
28 var ref = (o.direction == "up" || o.direction == "down") ? "top" : "left";
29 var motion = (o.direction == "up" || o.direction == "left") ? "pos" : "neg";
30
31 // Animation
32 var animation = {}, animation1 = {}, animation2 = {};
33 animation[ref] = (motion == "pos" ? "-=" : "+=") + o.distance;
34 animation1[ref] = (motion == "pos" ? "+=" : "-=") + o.distance * 2;
35 animation2[ref] = (motion == "pos" ? "-=" : "+=") + o.distance * 2;
36
37 // Animate
38 el.animate(animation, o.speed, o.easing);
39 for (var i = 1; i < o.times; i++) { // Shakes
40 el.animate(animation1, o.speed, o.easing).animate(animation2, o.speed, o.easing);
41 };
42 el.animate(animation1, o.speed, o.easing).
43 animate(animation, o.speed / 2, o.easing, function(){ // Last shake
44 el.css(props); // Restore
45 if(o.callback) o.callback.apply(this, arguments); // Callback
46 });
47 });
48 };
49 })(jQuery);
2 $.fn.shake = function(o) {
3 if (typeof o === 'function')
4 o = {callback: o};
5 // Set options
6 var o = $.extend({
7 direction: "left",
8 distance: 20,
9 times: 3,
10 speed: 140,
11 easing: "swing"
12 }, o);
13
14 return this.each(function() {
15
16 // Create element
17 var el = $(this), props = {
18 position: el.css("position"),
19 top: el.css("top"),
20 bottom: el.css("bottom"),
21 left: el.css("left"),
22 right: el.css("right")
23 };
24
25 el.css("position", "relative");
26
27 // Adjust
28 var ref = (o.direction == "up" || o.direction == "down") ? "top" : "left";
29 var motion = (o.direction == "up" || o.direction == "left") ? "pos" : "neg";
30
31 // Animation
32 var animation = {}, animation1 = {}, animation2 = {};
33 animation[ref] = (motion == "pos" ? "-=" : "+=") + o.distance;
34 animation1[ref] = (motion == "pos" ? "+=" : "-=") + o.distance * 2;
35 animation2[ref] = (motion == "pos" ? "-=" : "+=") + o.distance * 2;
36
37 // Animate
38 el.animate(animation, o.speed, o.easing);
39 for (var i = 1; i < o.times; i++) { // Shakes
40 el.animate(animation1, o.speed, o.easing).animate(animation2, o.speed, o.easing);
41 };
42 el.animate(animation1, o.speed, o.easing).
43 animate(animation, o.speed / 2, o.easing, function(){ // Last shake
44 el.css(props); // Restore
45 if(o.callback) o.callback.apply(this, arguments); // Callback
46 });
47 });
48 };
49 })(jQuery);